	package info.touret.winecellar.producer.client;

import info.touret.winecellar.producer.shared.ProducerProxy;

import java.util.List;
import java.util.Set;

import com.google.gwt.activity.shared.Activity;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.AcceptsOneWidget;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.requestfactory.shared.EntityProxyChange;
import com.google.web.bindery.requestfactory.shared.Receiver;
import com.google.web.bindery.requestfactory.shared.ServerFailure;
import com.google.web.bindery.requestfactory.shared.Violation;

public class ProducerUI extends Composite implements
		EntityProxyChange.Handler<ProducerProxy>, Activity {
	private ProducerRequestFactory requestFactory;
	private static ProducerUIUiBinder uiBinder = GWT
			.create(ProducerUIUiBinder.class);
	@UiField
	VerticalPanel addProducerPanel;
	@UiField(provided = true)
	Button submitbutton = new Button();
	@UiField(provided = true)
	TextBox domain = new TextBox();
	@UiField(provided = true)
	TextBox region = new TextBox();
	@UiField(provided = true)
	TextBox address = new TextBox();
	@UiField(provided = true)
	TextBox city = new TextBox();
	@UiField(provided = true)
	TextBox country = new TextBox();
	@UiField(provided = true)
	TextBox phone = new TextBox();
	@UiField(provided = true)
	TextBox name = new TextBox();

	interface ProducerUIUiBinder extends UiBinder<Widget, ProducerUI> {
	}

	
	/**
	 * The styles applied to the table.
	 */
	interface TableStyle extends CellTable.Style {
	}

	public ProducerUI() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	/**
	 * Custom listener for this widget.
	 */
	interface Listener {

		/**
		 * Called when the user selects a report.
		 * 
		 * @param report
		 *            the selected report
		 */
		void onReportSelected(ProducerProxy producer);
	}

	public ProducerUI(ProducerRequestFactory requestFactory) {
		this.requestFactory = requestFactory;
		initWidget(uiBinder.createAndBindUi(this));
	}

	private List<ProducerProxy> producers;

	public List<ProducerProxy> getProducers() {
		return producers;
	}

	public void setProducers(List<ProducerProxy> producers) {
		this.producers = producers;
	}

	@Override
	public String mayStop() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCancel() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onStop() {
		running = false;

	}

	private Boolean running;

	

	

	private ProducerRequest getRequest() {
		return requestFactory.producerRequest();
	}



	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.google.gwt.activity.shared.Activity#start(com.google.gwt.user.client
	 * .ui.AcceptsOneWidget, com.google.gwt.event.shared.EventBus)
	 */
	@Override
	public void start(AcceptsOneWidget panel, EventBus eventBus) {
		EntityProxyChange.registerForProxyType(eventBus, ProducerProxy.class,
				this);
		running = true;
		panel.setWidget(this);
	}

	@Override
	public void onProxyChange(EntityProxyChange<ProducerProxy> event) {
		// TODO Auto-generated method stub

	}

	private String startsWithSearch;

	@UiHandler("submitbutton")
	void onSubmitbuttonClick(ClickEvent event) {
		try {
			ProducerRequest r = getRequest();
			ProducerProxy proxy = r.create(ProducerProxy.class);
			// faire un wrapper
			proxy.setName(name.getValue());
			proxy.setCity(city.getValue());
			proxy.setDomain(domain.getValue());
			proxy.setCountry(country.getValue());
			proxy.setPhone(phone.getValue());
			// proxy.setZip(zip.)
			proxy.setRegion(region.getValue());
			proxy.setDomain(domain.getValue());
			proxy.setAddress(address.getValue());
			Receiver<Void> receiver = new Receiver<Void>() {
				@Override
				public void onViolation(Set<Violation> errors) {
					Window.alert("Les parametres sont incorrects");
				}
				@Override
				public void onSuccess(Void response) {
					Window.alert("Insertion OK");
					clearform();
				}
				@Override
				public void onFailure(ServerFailure error) {
					super.onFailure(error);
					Window.alert("Une erreur est surveune :-(");
				}
			};
			r.persist().using(proxy).fire(receiver);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void clearform() {
		name.setValue(null);
		domain.setValue(null);
		city.setValue(null);
		address.setValue(null);
		country.setValue(null);
		region.setValue(null);

	}


	@UiHandler("rollbackbutton")
	void onRollbackbuttonClick(ClickEvent event) {
		clearform();
	}


}
